package com.wangtongxiao.services;

import com.wangtongxiao.beans.Activity;
import com.wangtongxiao.dto.ResultPageDTO;
import com.wangtongxiao.mapper.ActivityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class ActivityServicesImp implements ActivityServices {

    @Autowired
    ActivityMapper activityMapper;

    @Override
    public ArrayList<Activity> getAll(ResultPageDTO resultPageDTO) {
//        获取当前页
        int currentPage = resultPageDTO.getCurrentPage();
//        获取每页显示行数
        int rowsPerPage = resultPageDTO.getRowsPerPage();
//        计算起始页，共sql使用
        int i = (currentPage - 1) * rowsPerPage;
        resultPageDTO.setCurrentPage(i);

//查询总行数
        int count = activityMapper.getCount(resultPageDTO);
//        设置总行数
        resultPageDTO.setTotalRows(count);
//        假设没有余数
        resultPageDTO.setTotalPages(count / rowsPerPage);
        if (count % rowsPerPage != 0) {
            resultPageDTO.setTotalPages(count / rowsPerPage + 1);
        }

        return activityMapper.getAll(resultPageDTO);
    }
}
